package cn.wh.ymcc.mq.consumer;

import cn.wh.ymcc.service.IPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 支付超时取消支付单
 */
@Component
@RocketMQMessageListener(
        consumerGroup = "pay-timeout-consumer",
        topic = "pay-timeout-topic",
        selectorExpression = "pay-timeout-tag",
        messageModel = MessageModel.BROADCASTING//  广播模式
)
@Slf4j
public class PayTimeOutConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IPayOrderService payOrderService;


    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("订单微服务：接收到支付超时消息");
        byte[] body = messageExt.getBody();
        if (body == null || body.length == 0){
            return;//消息不对,直接签收
        }
        String orderNo = new String(body, StandardCharsets.UTF_8);
        payOrderService.cancelOrder(orderNo);
    }
}
